package app.netant.book.ws65;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
import app.netant.book.base.BaseEntity;
import app.netant.book.base.BasePage;
import cn.lily.com.enviroment.Enviroment;

public class Ws65Page extends BasePage {
	
	private String type;

	public Ws65Page(String netSite) {
		super(netSite);
		this.tableKey = "ws65";
		this.selectKey = "section.list .list_l ul li .title span.shujia a";
		this.preHref = "http://www.65ws.com";
		this.analyseType();
	}
	
	public void analyseType(){
		Elements parts = this.select("section.list .list_l .ltop h2");
		if(parts.size()>0){
			this.type = parts.get(0).text();
		}
	}

	@Override
	public String buildHref(Element element) {		
		return element.attr("href");
	}

	@Override
	public BaseEntity buildEntity() {
		return new Ws65Entity();
	}

	@Override
	public BaseDetail buildDetail(String href) {
		return new Ws65Detail(href);
	}

	@Override
	public void afterEntity(BaseEntity entity, Element element) {
		Elements tagEle = element.parent().parent().parent().select("div.data span");
		for(int i=0;i<tagEle.size();i++){
			String tags = tagEle.get(i).text();
			int idx = tags.indexOf("：");
			if(idx>0){
				String tagName = tags.substring(0,idx);
				String tagValue = tags.substring(idx+1);
				if("更新".equals(tagName)){
					tagValue = "20"+tagValue;
				}
				System.out.println("【Tag】:"+tagName+"="+tagValue);
				entity.setTag(tagName, tagValue);
			}
		}
		
		if(this.type != null && !"".equals(type)){
			System.out.println("【Type】:"+type);
			entity.setNovelType(type);
		}
		
		Element memoEle = element.parent().parent().parent().child(3);
		String memo = memoEle.text();
		if(memo != null && !"".equals(memo)){
			System.out.println("【Memo】:"+memo);
			entity.setNovelMemo(memo);
		}
	}
	
	public static void main(String[] args) {
		Enviroment.createEnv();
		Enviroment.getEnv().getLogBean().beginLog();
		Ws65Page page = new Ws65Page("http://www.65ws.com/flsort/sort1/1.html");
		page.analyseHref();
	}
	
}
